﻿var styleChange = {
	pause: {}, 
	play: {}
};

//播放按钮
styleChange.play.change = function(){
	$('#pauseBtn').show();
	$('#playBtn').hide();
};

//暂停按钮
styleChange.pause.change = function(){
	$('#playBtn').show();
	$('#pauseBtn').hide();
};

// 格式化秒数到时间格式
Number.prototype.formatTime=function(){
    // 计算
    var i = 0, s = parseInt(this);
    if(s >= 60){
        i = parseInt(s / 60);
        s = parseInt(s % 60);
        if(i > 60) {
            h = parseInt(i / 60);
            i = parseInt(i % 60);
        }
    }
    // 补零
    var zero = function(v){
        return (v >> 0) < 10? "0" + v: v;
    };
    return [zero(i),zero(s)].join(":");
};

$(function(){
	var dur, c, d, cur;
	var $audio = $("audio");//获取音频	
	var progressWrap = $('#progressWrap');//进度容器
    var progressBar = $("#progressBar");//进度条
    var progressBtn = $("#progressBtn");//进度控制按钮
    var startPos, endPos;
    var widthX = 0;
    var offsetX = 0;
    var moveTo = 0;
    var width = parseInt(progressWrap.width());
    var left = width + progressBtn.position().left;
    
	$audio[0].play();//自动播放
	
	FastClick.attach(document.body);//快速点击，消除移动端点击延迟的300ms时间
	
	//拖动进度条
    /*(function(){
        // 滑动开始
        progressWrap.on('touchstart', function (event){
            event.preventDefault();   

            var touch = event.touches[0];
            //取第一个touch的坐标值
            startPos = {
                x: touch.pageX,
                y: touch.pageY,
                time: +new Date
            };
        });

        // 移动
        progressWrap.on('touchmove', function (event){
            event.preventDefault();   

            //当屏幕有多个touch或者页面被缩放过，就不执行move操作
            if(event.touches.length > 1 || event.scale && event.scale !== 1){
                return;
            }
            var touch = event.touches[0];
            endPos = {
                x: touch.pageX - startPos.x,
                y: touch.pageY - startPos.y
            };
         
            //执行操作，使元素移动
            progressBar.css("width", widthX + endPos.x + "px");
            progressBtn.animate({left: offsetX + endPos.x}, 0);
            
            if(progressBtn.position().left <= 0){
                progressBar.css("width", 0);
                progressBtn.css("left", 0);
            }
            if(progressBar.width() >= width){
                progressBar.css("width", width);
                progressBtn.css("left", left + "px");
            }
        });

        // 滑动释放
        progressWrap.on('touchend', function (event){
            //滑动的持续时间
            var duration = +new Date - startPos.time;
            
            //记录当前宽度和位置
            if(Number(duration) > 100){
                widthX = parseInt(progressBar.width());
                offsetX = parseInt(progressBtn.position().left);
                $audio[0].currentTime = offsetX/width * $audio[0].duration;
            }
        });
    }());*/
	
	//当播放位置改变时	
	$audio.on("timeupdate", function(){
		c = this.currentTime;
		cur = c.formatTime();

		$('#currentTime').text(cur);
	});
	
	//当音频已开始播放时		
	$audio.on("playing", function(){
		d = this.duration;
		dur = d.formatTime();
		styleChange.play.change();
		$('#totalTime').text(dur);
		$(".music-img").removeClass("music-pause").addClass("music-img_flash");
		$(".music-img__arm").removeClass("music-pause").addClass("music-img__arm_flash");
	});
	
	//当音频已到达结尾时
	$audio.on("ended", function(){
		this.currentTime = 0;
		var begin = this.currentTime.formatTime();
		widthX = 0;
        offsetX = 0;

		$audio[0].pause();
		styleChange.pause.change();
		$('#currentTime').text(begin);
		progressBar.css("width", 0);
		progressBtn.animate({left: 0}, 0);
		$(".music-img").addClass("music-pause");
		$(".music-img__arm").addClass("music-pause");
	});
	
	//播放
	$('#playBtn').on("tap", function(){
		$audio[0].play();
		styleChange.play.change();
		$(".music-img").removeClass("music-pause").addClass("music-img_flash");
		$(".music-img__arm").removeClass("music-pause").addClass("music-img__arm_flash");
	}); 
		
	//暂停
	$('#pauseBtn').on("tap", function(){
		$audio[0].pause();
		styleChange.pause.change();
		$(".music-img").addClass("music-pause");
		$(".music-img__arm").addClass("music-pause");
	});
		
	//下一首
	/*$('#nextBtn').on("tap", function(){
		$audio[0].pause();
		$audio[0].currentTime = 0;
		if(false){
			$audio[0].play();
		}
		else{
			alert('已经是最后一首了哦！');
		}
	});
			
	//上一首
	$('#prevBtn').on("tap", function(){
		$audio[0].pause();
		$audio[0].currentTime = 0;
		if(false){
			$audio[0].play();
		}
		else{
			alert('已经是第一首了哦！');
		}
	});*/
			
	//进度条
	$("#progressWrap").on("click", function(event){
		var moveTo = event.pageX - $(this).offset().left;
		$audio[0].currentTime = moveTo/parseInt($(this).css("width")) * $audio[0].duration;
		$(this).find(".progress-bar").css("width", moveTo + "px");
		$("#progressBtn").animate({left: moveTo + "px"}, 0);
	});

	//投票
	$("#voteBtn").on("tap", function(){
		var _this = $(this);
        var totalNum = parseInt(_this.closest(".songs-info").find(".songs-text__total").text());
        var voteLogId = _this.attr("voteLogId");
        var voteSongId= _this.attr("voteSongId");
		$.ajax({
          type: "get",
          url: "vote?voteLogId="+voteLogId+"&voteSongId="+voteSongId,
          dataType: "json",
          success: function(data) {
                var returnFlag = data.returnFlag; //数据是否返回成功
                var code = data.code; //是否参与过报名
                var totalNum = $(".msg__info__total").text();
                
                if(code == "0"){
                    alert("感谢您的投票！");
                    _this.addClass("voted-btn");
                    $(".msg__info__total").text(++totalNum);
                }
                else{
                    alert("您已经投过票了！");
                }
            },
            error: function(msg) {
                alert("请求数据失败！");
            }
        });
	});
});